/*
 * ComerciosBusquedaFrame.java
 *
 * Created on 16 de abril de 2008, 11:45 PM
 */
package org.comercios.registros;

import org.mybeans.Sesion;
import org.mybeans.ComerciosBeans;
  
import org.mybeans.ComerciosInscripcionBeans;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author  avbravo
 */
public class ComerciosBajadosFrame extends javax.swing.JFrame {

    Sesion sesion;
    int fila = -1;
    DefaultTableModel modelo;
    ComerciosBeans comerciosBeans;
    ComerciosInscripcionBeans comerciosInscripcionBeans;

    /** Creates new form ComerciosBusquedaFrame */
    public ComerciosBajadosFrame() {
        initComponents();
        sesion = new Sesion();
        comerciosBeans = new ComerciosBeans();
        /*
         * Fondo
         */

         
         
         

         
         
        /*
         * fondo
         */

        //String sql = "select comercios.*, contribuyentes.contribuyente from comercios, contribuyentes where comercios.idmunicipio ='" + sesion.getIdmunicipio() + "' and comercios.activo ='si' and comercios.cedula = contribuyentes.cedula order by comercio";
        String sql = "select comercios.idcomercio, comercios.comercio, contribuyentes.cedula, contribuyentes.contribuyente, " +
                " comerciosbaja.fecha, comerciosbaja.motivo "+
                " from comercios, contribuyentes, comerciosbaja " +
                " where comercios.idmunicipio ='" + sesion.getIdmunicipio() + "' " +
                " and comercios.activo ='no' and comercios.cedula = contribuyentes.cedula" +
                " and comerciosbaja.idcomercio = comercios.idcomercio" +
                " and comerciosbaja.activo='si'" +
                " and comerciosbaja.idmunicipio = comercios.idmunicipio" +
                " and contribuyentes.idmunicipio = comercios.idmunicipio" +
                " order by comercio";
    
        CargarTodosRegistros(sql);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jXTable1 = new org.jdesktop.swingx.JXTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("COMERCIOS BAJADOS");

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel2.setOpaque(false);

        jXTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null},
                {null, null, null, null, null, null}
            },
            new String [] {
                "codigo", "comercio", "contribuyente", "cedula", "fecha", "motivo"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jXTable1);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 724, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 267, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(65, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ComerciosBajadosFrame().setVisible(true);
            }
        });
    }

   
    public void CargarTodosRegistros(String sql) {
        try {

            if (sesion.Conectar() == false) {
                JOptionPane.showMessageDialog(this, "No se pudo establecer la conexion", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                return;
            }

            PreparedStatement psquery = sesion.con.prepareStatement(sql);
            ResultSet rs = psquery.executeQuery();

            modelo = new DefaultTableModel();
            // Creamos las columnas.
            modelo.addColumn("codigo");
            modelo.addColumn("comercio");
            modelo.addColumn("contribuyente");
            modelo.addColumn("cedula");
            modelo.addColumn("fecha");
            modelo.addColumn("motivo");

// Bucle para cada resultado en la consulta
            while (rs.next()) {
                // Se crea un array que será una de las filas de la tabla.
                Object[] fila = new Object[6]; // Hay tres columnas en la tabla

                fila[0] = rs.getString("idcomercio");
                fila[1] = rs.getString("comercio");
                fila[2] = rs.getString("contribuyente");
                fila[3] = rs.getString("cedula");
                fila[4] = rs.getString("fecha");
                fila[5] = rs.getString("motivo");
                modelo.addRow(fila);

            }
            this.jXTable1.setModel(modelo);
            jXTable1.setVisible(true);
            //aris++
            this.jXTable1.addMouseListener(new MouseAdapter() {

                public void mouseClicked(MouseEvent e) {

                    fila = jXTable1.rowAtPoint(e.getPoint());
                    int columna = jXTable1.columnAtPoint(e.getPoint());
                    if ((fila > -1) && (columna > -1)) {
                        String contribuyente = String.valueOf(jXTable1.getValueAt(fila, 2));
                        String cedula = String.valueOf(jXTable1.getValueAt(fila, 3));

                      

                    }
                }
            });

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Error " + ex.getMessage(), "Error. CargarTodosRegistros()", JOptionPane.ERROR_MESSAGE);
        }
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private org.jdesktop.swingx.JXTable jXTable1;
    // End of variables declaration//GEN-END:variables
}
